$(function() {
	var status = $("#status").val();
	var season = $("#season"), disease = $("#disease"), priority = $("#priority"), age = $("#age"), ruleType = $("#ruleType"), subType = $("#subType"), value = $("#value"), tips = $("#tips"), age1 = $("#age1"), age2 = $("#age2"), sex = $("#sex"), month = $("#month"), di = $("#di"), rank = $("#rank"), allFields = $(
			[]).add(season).add(disease).add(priority).add(age).add(ruleType)
			.add(subType).add(tips).add(age1).add(age2).add(sex).add(month)
			.add(di).add(rank).add(value), validateTips = $(".validateTips");

	function updateTips(t) {
		validateTips.text(t).addClass("ui-state-highlight");
		setTimeout(function() {
			validateTips.removeClass("ui-state-highlight", 1500);
		}, 500);
	}

	function checkLength(o, n, min, max) {
		if (o.val().length > max || o.val().length < min) {
			o.addClass("ui-state-error");
			updateTips("" + n + " 的长度必须在 " + min + " 和 " + max + " 之间。");
			return false;
		} else {
			return true;
		}
	}

	function checkEmpity(o, n) {
		if (o.val() != null && o.val().length > 0) {
			return true
		} else {
			o.addClass("ui-state-error");
			updateTips("" + n + " 不能为空 ");
			return false;
		}
	}

	function checkRegexp(o, regexp, n) {
		if (!(regexp.test(o.val()))) {
			o.addClass("ui-state-error");
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	$("#dialog-form").dialog(
			{
				autoOpen : false,
				height : 650,
				width : 550,
				modal : true,
				buttons : {
					"添加" : function() {
						var bValid = true;
						allFields.removeClass("ui-state-error");
						// 开始校验
						if (status == 1) {
							bValid = bValid && checkEmpity(season, "season");
							bValid = bValid && checkEmpity(disease, "disease");
							bValid = bValid
									&& checkEmpity(priority, "priority");
							bValid = bValid
									&& checkRegexp(priority, /^[1-9]\d*$/,
											"priority必须为数字。");
						} else if (status == 2) {
							bValid = bValid && checkEmpity(season, "season");
							bValid = bValid && checkEmpity(subType, "subType");
							bValid = bValid && checkEmpity(value, "value");
							bValid = bValid && checkEmpity(disease, "disease");
							bValid = bValid
									&& checkEmpity(priority, "priority");
							bValid = bValid && checkEmpity(tips, "tips");
							bValid = bValid
									&& checkRegexp(priority, /^[1-9]\d*$/,
											"priority必须为数字。");
							bValid = bValid
									&& checkRegexp(value, /^[1-9]\d*$/,
											"value必须为数字。");
						} else if (status == 3) {
							bValid = bValid && checkEmpity(age1, "age1");
							bValid = bValid && checkEmpity(age2, "age2");
							bValid = bValid && checkEmpity(month, "month");
							bValid = bValid && checkEmpity(di, "di");
							bValid = bValid && checkEmpity(rank, "rank");
							bValid = bValid
									&& checkRegexp(age1, /^[1-9]\d*$/,
											"age1必须为数字。");
							bValid = bValid
									&& checkRegexp(age2, /^[1-9]\d*$/,
											"age2必须为数字。");
							bValid = bValid
									&& checkRegexp(month, /^[1-9]\d*$/,
											"month必须为数字。");
							bValid = bValid
									&& checkRegexp(rank, /^[1-9]\d*$/,
											"rank必须为数字。");
						}
						// 如果校验成功
						if (bValid) {
							var param = {
								"status" : status,
								"season" : season.val(),
								"disease" : disease.val(),
								"priority" : priority.val(),
								"age" : age.val(),
								"ruleType" : ruleType.val(),
								"subType" : subType.val(),
								"value" : value.val(),
								"tips" : tips.val(),
								"age1" : age1.val(),
								"age2" : age2.val(),
								"sex" : sex.val(),
								"month" : month.val(),
								"di" : di.val(),
								"rank" : rank.val()
							};
							API.addDiseaseWeather(param, function(res) {
								if (res.code == '0') {
									window.location.reload();
								} else {
									alert(res.desc);
									window.location.reload();
								}
							});
						}
					},
					Cancel : function() {
						$(this).dialog("close");
					}
				},
				close : function() {
					allFields.val("").removeClass("ui-state-error");
				}
			});
	$("#add-class").button().click(function() {
		$("#dialog-form").dialog("open");
	});
});
$("#dialog-confirm").dialog({
	autoOpen : false,
	height : 150,
	width : 400,
	modal : true,
	buttons : {
		"确定删除" : function() {
			var param = {
				"id" : $("#id").val(),
				"status" : $("#status").val()
			};
			API.deleteDiseaseWeather(param, function(res) {
				if (res.code == '0') {
					window.location.reload();
				} else {
					alert(res.desc);
					window.location.reload();
				}
			});
		},
		Cancel : function() {
			$(this).dialog("close");
		}
	}
});

function deleteDiseaseWeather(id) {
	$("#id").val(id);
	$("#dialog-confirm").dialog("open");
}

$("#importing").button().hide();
$("#import").button().click(function(){
	$("#file").click();
});
$("#file").change(function(){
	$("#importing").button().show();
	$("#import").button().hide();
	var url ="/icd/disease_weather/import_data.do";
	var status = $("#status").val();
	importFile("#file",url,status);
});	

function exportTxt(status) {
	window.location.href = "/icd/disease_weather/export_data.do?status="
			+ status;
}
$("#export").button();

function preUpdate(obj) {
	$(obj).hide();
	$(obj).next("input").show();
	$(obj).next("input").focus();
}

function update(obj) {
	// inspectionUpdateTestingClass

	var param = {
		id : $(obj).attr("dataid"),
		status : $("#status").val()
	};

	if ($(obj).attr("id") == "updateSeason") {
		param.season = $(obj).val();
	}

	if ($(obj).attr("id") == "updateDisease") {
		param.disease = $(obj).val();
	}

	if ($(obj).attr("id") == "updatePriority") {
		param.priority = $(obj).val();
	}

	if ($(obj).attr("id") == "updateAge") {
		param.age = $(obj).val();
	}

	if ($(obj).attr("id") == "updateRuleType") {
		param.ruleType = $(obj).val();
	}

	if ($(obj).attr("id") == "updateSubType") {
		param.subType = $(obj).val();
	}

	if ($(obj).attr("id") == "updateValue") {
		param.value = $(obj).val();
	}

	if ($(obj).attr("id") == "updateTips") {
		param.tips = $(obj).val();
	}

	if ($(obj).attr("id") == "updateAge1") {
		param.age1 = $(obj).val();
	}

	if ($(obj).attr("id") == "updateAge2") {
		param.age2 = $(obj).val();
	}

	if ($(obj).attr("id") == "updateSex") {
		param.sex = $(obj).val();
	}

	if ($(obj).attr("id") == "updateMonth") {
		param.month = $(obj).val();
	}

	if ($(obj).attr("id") == "updateDi") {
		param.di = $(obj).val();
	}

	if ($(obj).attr("id") == "updateRank") {
		param.rank = $(obj).val();
	}

	API.updateDiseaseWeather(param, function(res) {
		UTIL.releaseButton(obj);
		if (res.code == '0') {
			window.location.reload();
		} else {
			alert(res.desc);
			window.location.reload();
		}
	});
}